package com.base.mine.presenter;

import android.app.Activity;
import android.text.TextUtils;


import com.base.commonlib.base.BaseApp;
import com.base.commonlib.base.presenter.BasePresenter;
import com.base.commonlib.bean.DaoSession;
import com.base.commonlib.bean.RoadEntity;
import com.base.commonlib.bean.RoadEntityDao;
import com.base.commonlib.bean.UserRoad;
import com.base.commonlib.bean.UserRoadDao;
import com.base.commonlib.event.RoadEvent;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.utils.CallBackImpl;
import com.base.commonlib.utils.MineRoadUtil;
import com.base.commonlib.utils.RxBus2;
import com.base.mine.view.RoadContract;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;

/**
 * Created by weikailiang on 2019/6/11.
 */

public class RoadPresenter extends BasePresenter<RoadContract> {
    public List<RoadEntity> mRoads = new ArrayList<>();
    private List<RoadEntity> mRoads_initial = new ArrayList<>();

    public List<RoadEntity> mMineRoads;
    public RoadPresenter(RoadContract view, Activity activity) {
        super(view, activity);
    }

    @Override
    public void start() {
        super.start();
        init();
    }

    private void init() {
        mMineRoads = BaseApp.getMineRoad(mUser.getUserId());
        mRoads.addAll(BaseApp.getMineRoadEntities());
        mRoads_initial.addAll(BaseApp.getMineRoadEntities());
        mView.showRoads();
    }


    public void search(String keyWord) {
        mRoads.clear();
        if (TextUtils.isEmpty(keyWord)) {
            mRoads.addAll(mRoads_initial);
        } else {
            for (RoadEntity item : mRoads_initial) {
                if (item.getRoadName().contains(keyWord)) {
                    mRoads.add(item);
                }
            }
        }
        mView.showRoads();


    }

    public void refresh() {
        mView.showPb();
        Disposable enqueue = RealNetWorkUtil.postContent().method("api-ll/dropdowm/selectRoadInfo").content(new Object())
                .build().enqueueForArray(RoadEntity.class, new CallBackImpl<List<RoadEntity>>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb();
                    }

                    @Override
                    public void onNext(List<RoadEntity> roads) {
                        mView.hidePb();
                        BaseApp.insertRoad(roads);
                        mRoads_initial.clear();
                        mRoads_initial.addAll(roads);
                        mRoads.clear();
                        mRoads.addAll(roads);
                        mView.showMes("更新成功!");
                    }
                });

        mCompositeDisposable.add(enqueue);
    }


    public void setUserRoads() {
       BaseApp.setUserRoad(mUser.getUserId(),mMineRoads);

    }
}
